<template>
    <div class="container-fluid" v-loading="loading">

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header"><h4>课件创建</h4></div>
                    <div class="card-body">

                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="title">标题</label>
                                    <input type="text" class="form-control" v-model="form.title" placeholder="标题..">
                                </div>
                                <div class="form-group">
                                    <label for="summary">摘要</label>
                                    <el-input
                                            type="textarea"
                                            placeholder="请输入内容"
                                            v-model="form.describe"
                                            :rows="4"
                                            maxlength="100"
                                            show-word-limit>
                                    </el-input>
                                </div>
                                <div class="form-group">
                                    <label for="imageUrl">封面图片</label>
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/api/upload"
                                            :show-file-list="false"
                                            accept="image/gif, image/jpeg, image/png"
                                            :on-success="handleImgUrlSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </div>
                                <div class="form-group" style="margin-left:10px;">
                                    <p><button type="submit" class="btn btn-primary" v-on:click="submit(0)">确定创建</button></p>
                                    <button type="submit" class="btn btn-primary" v-on:click="submit(1)">加推送</button>
                                </div>
                            </div>
                            <div class="col-md-8">
                                <div class="form-group">
                                    <label for="imageUrl">课件</label>
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/api/upload?module=folder"
                                            :show-file-list="false"
                                            accept="application/pdf,application/msword, application/msword3, application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation"
                                            :on-success="handleFileSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <i v-if="form.videoUrl" class="el-icon-document el-icon-document-icon"></i>
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                    <a v-if="form.videoUrl" :href="form.videoUrl" target="_blank"><i class="el-icon-s-promotion el-icon-s-promotion-icon"></i></a>
                                </div>
                                <div class="form-group">
                                    <label for="content">内容</label>
                                    <vue-ueditor-wrap v-model="form.body" :config="editorOption"></vue-ueditor-wrap>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import common from '../../api/public.js';
    export default {
        name: "DocumentCreateComponent",
        data() {
            return {
                form:{
                    title: '',
                    describe:'',
                    imageUrl:'',
                    videoUrl:'',
                    status:1,
                    verifyStatus:1,
                    pushStatus:0
                },
                editorOption: {},
                loading:false
            }
        },
        methods:{
            ...common,
            submit:function (pushStatus) {
                let self = this;
                let uri = "document/store";
                if (!self.form.title) {
                    self.$message.error("必须填写标题");
                    return;
                }
                if (!self.form.describe) {
                    self.$message.error("必须填写摘要");
                    return;
                }
                if (!self.form.imageUrl) {
                    self.$message.error("必须上传封面图");
                    return;
                }
                if (!self.form.videoUrl) {
                    self.$message.error("必须上传课件");
                    return;
                }
                if (!self.form.body) {
                    self.$message.error("必须填写内容");
                    return;
                }
                self.form.pushStatus = pushStatus;
                self.loading = true;
                axios.post(uri,self.form).then(function (result) {
                    self.loading = false;
                    if (result.data) {
                        self.openUri('document/index');
                    } else {
                        self.$message.error("创建失败课件！");
                    }
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            handleImgUrlSuccess: function (response) {
                let self = this;
                if (response.code === 200) {
                    self.form.imageUrl = response.url;
                }
            },
            initUpload(){
                this.editorOption =
                    {
                        // 编辑器不自动被内容撑高
                        autoHeightEnabled: false,
                        // 初始容器高度
                        initialFrameHeight:200,
                        // 初始容器宽度
                        initialFrameWidth: '100%',
                        // 上传文件接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
                        serverUrl: '/api/upload',
                        // UEditor 资源文件的存放路径，如果你使用的是 vue-cli 生成的项目，通常不需要设置该选项，vue-ueditor-wrap 会自动处理常见的情况，如果需要特殊配置，参考下方的常见问题2
                        UEDITOR_HOME_URL: '/static/UEditor/'
                    };
            }
        },
        mounted: function () {
            this.initUpload();
        },
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .el-upload__input{
        display: none;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
    .avatar {
        width: 120px;
        height: 120px;
        display: block;
    }
    .el-icon-document-icon{
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
    .el-icon-s-promotion-icon{
        font-size: 28px;
        color: #8c939d;
        width: 60px;
        height: 60px;
        line-height: 60px;
        text-align: center;
    }
</style>
